SlideShare a Scribd company logo
Class No.15  Data Structures http://ecomputernotes.com
Level-order Traversal There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously. In level-order traversal, we visit the nodes at each level before proceeding to the next level. At each level, we visit the nodes in a left-to-right order. http://ecomputernotes.com
Level-order Traversal Level-order: 14 4 15 3 9 18 7 16 20 5 17 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal How do we do level-order traversal? Surprisingly, if we use a queue instead of a stack, we can visit the nodes in level-order. Here is the code for level-order traversal: http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() )  { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
Level-order Traversal Queue: 14 Output: 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 4 15 Output: 14 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 15 3 9 Output: 14 4 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 3 9 18 Output: 14 4 15 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 9 18 Output: 14 4 15 3  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 18 7 Output: 14 4 15 3 9  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 7 16 20 Output: 14 4 15 3 9 18  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 16 20 5 Output: 14 4 15 3 9 18 7  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 20 5 17 Output: 14 4 15 3 9 18 7 16  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 5 17 Output: 14 4 15 3 9 18 7 16 20  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: 17 Output: 14 4 15 3 9 18 7 16 20 5  14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Level-order Traversal Queue: Output: 14 4 15 3 9 18 7 16 20 5 17 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
Storing other Type of Data The examples of binary trees so far have been storing integer data in the tree node. This is surely not a requirement. Any type of data can be stored in a tree node. Here, for example, is the C++ code to build a tree with character strings.  http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;,  &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ )  insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info  << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node );  }  http://ecomputernotes.com
Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info  << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node );  }  http://ecomputernotes.com
Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info  << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node );  }  http://ecomputernotes.com
Binary Search Tree with Strings Output: abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. Inorder prints the left subtree, then the node finally the right subtree. http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. Inorder prints the left subtree, then the node finally the right subtree. Building a BST and doing an inorder traversal leads to a sorting algorithm. http://ecomputernotes.com
Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. Inorder prints the left subtree, then the node finally the right subtree. Building a BST and doing an inorder traversal leads to a sorting algorithm. http://ecomputernotes.com
Deleting a node in BST As is common with many data structures, the hardest operation is deletion. Once we have found the node to be deleted, we need to consider several possibilities. If the node is a  leaf , it can be deleted immediately. http://ecomputernotes.com
Deleting a node in BST If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node and connect to inorder successor. 6 2 4 3 1 8 http://ecomputernotes.com
Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8  http://ecomputernotes.com
Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8 6 2 3 1 8   http://ecomputernotes.com
Deleting a node in BST The complicated case is when the node to be deleted has both left and right subtrees. The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node. http://ecomputernotes.com
Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder  successor http://ecomputernotes.com
Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder  successor Inorder successor will be the left-most node in the right subtree of 2. The inorder successor will not have a left child because if it did, that child would be the left-most node. http://ecomputernotes.com
Deleting a node in BST Delete(2): copy data from inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
Deleting a node in BST Delete(2): remove the inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
Deleting a node in BST Delete(2)  6 3 5 4 1 8  6 3 5 3 1 8 4 http://ecomputernotes.com

More Related Content

PPT
Computer notes - Binary Search Tree with Strings
PDF
Clojure 1.1 And Beyond
PDF
The Magnificent Seven
PDF
Rich and Snappy Apps (No Scaling Required)
PDF
Os Fetterupdated
PDF
Extreme JavaScript Performance
PDF
Better Software: introduction to good code
PDF
Php radomize
Computer notes - Binary Search Tree with Strings
Clojure 1.1 And Beyond
The Magnificent Seven
Rich and Snappy Apps (No Scaling Required)
Os Fetterupdated
Extreme JavaScript Performance
Better Software: introduction to good code
Php radomize

Similar to computer notes - Data Structures - 15 (20)

PPT
computer notes - Data Structures - 14
PPT
Computer notes - Recursive
PPT
computer notes - Data Structures - 13
PPT
Php Sq Lite
PPT
Php My Sql
ODP
State Machines to State of the Art
ODP
Scala 2 + 2 > 4
PPTX
Oscon 2010 Specs talk
PPT
Arduino section programming slides
PPT
Embedded Typesafe Domain Specific Languages for Java
PPTX
(Slightly) Smarter Smart Pointers
PPTX
Groovy
DOCX
#include iostream#include d_node.h #include d_nodel.h.docx
PDF
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
PPTX
Shkrubbel for Open Web Camp 3
PPT
Arduino sectionprogramming slides
PPT
computer notes - Data Structures - 16
PPT
Falcon初印象
PDF
MCE^3 - Hannes Verlinde - Let The Symbols Do The Work
PPT
C++ programming
computer notes - Data Structures - 14
Computer notes - Recursive
computer notes - Data Structures - 13
Php Sq Lite
Php My Sql
State Machines to State of the Art
Scala 2 + 2 > 4
Oscon 2010 Specs talk
Arduino section programming slides
Embedded Typesafe Domain Specific Languages for Java
(Slightly) Smarter Smart Pointers
Groovy
#include iostream#include d_node.h #include d_nodel.h.docx
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Shkrubbel for Open Web Camp 3
Arduino sectionprogramming slides
computer notes - Data Structures - 16
Falcon初印象
MCE^3 - Hannes Verlinde - Let The Symbols Do The Work
C++ programming
Ad

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 4
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 30
computer notes - Data Structures - 39
computer notes - Data Structures - 11
computer notes - Data Structures - 20
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 31
computer notes - Data Structures - 4
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 22
computer notes - Data Structures - 35
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
Ad

Recently uploaded (20)

PDF
Solaris Resources Presentation - Corporate August 2025.pdf
DOCX
80 DE ÔN VÀO 10 NĂM 2023vhkkkjjhhhhjjjj
PPTX
chapter 2 entrepreneurship full lecture ppt
PDF
Robin Fischer: A Visionary Leader Making a Difference in Healthcare, One Day ...
PPTX
IITM - FINAL Option - 01 - 12.08.25.pptx
PDF
Environmental Law Communication: Strategies for Advocacy (www.kiu.ac.ug)
PDF
Introduction to Generative Engine Optimization (GEO)
PPTX
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
PDF
Charisse Litchman: A Maverick Making Neurological Care More Accessible
PDF
Family Law: The Role of Communication in Mediation (www.kiu.ac.ug)
PDF
#1 Safe and Secure Verified Cash App Accounts for Purchase.pdf
PDF
Kishore Vora - Best CFO in India to watch in 2025.pdf
DOCX
Center Enamel A Strategic Partner for the Modernization of Georgia's Chemical...
PDF
Ron Thomas - Top Influential Business Leaders Shaping the Modern Industry – 2025
PPT
Lecture 3344;;,,(,(((((((((((((((((((((((
PPTX
operations management : demand supply ch
PPTX
BUSINESS CYCLE_INFLATION AND UNEMPLOYMENT.pptx
DOCX
Center Enamel Powering Innovation and Resilience in the Italian Chemical Indu...
PPTX
basic introduction to research chapter 1.pptx
PDF
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...
Solaris Resources Presentation - Corporate August 2025.pdf
80 DE ÔN VÀO 10 NĂM 2023vhkkkjjhhhhjjjj
chapter 2 entrepreneurship full lecture ppt
Robin Fischer: A Visionary Leader Making a Difference in Healthcare, One Day ...
IITM - FINAL Option - 01 - 12.08.25.pptx
Environmental Law Communication: Strategies for Advocacy (www.kiu.ac.ug)
Introduction to Generative Engine Optimization (GEO)
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
Charisse Litchman: A Maverick Making Neurological Care More Accessible
Family Law: The Role of Communication in Mediation (www.kiu.ac.ug)
#1 Safe and Secure Verified Cash App Accounts for Purchase.pdf
Kishore Vora - Best CFO in India to watch in 2025.pdf
Center Enamel A Strategic Partner for the Modernization of Georgia's Chemical...
Ron Thomas - Top Influential Business Leaders Shaping the Modern Industry – 2025
Lecture 3344;;,,(,(((((((((((((((((((((((
operations management : demand supply ch
BUSINESS CYCLE_INFLATION AND UNEMPLOYMENT.pptx
Center Enamel Powering Innovation and Resilience in the Italian Chemical Indu...
basic introduction to research chapter 1.pptx
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...

computer notes - Data Structures - 15

  • 1. Class No.15 Data Structures http://ecomputernotes.com
  • 2. Level-order Traversal There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously. In level-order traversal, we visit the nodes at each level before proceeding to the next level. At each level, we visit the nodes in a left-to-right order. http://ecomputernotes.com
  • 3. Level-order Traversal Level-order: 14 4 15 3 9 18 7 16 20 5 17 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 4. Level-order Traversal How do we do level-order traversal? Surprisingly, if we use a queue instead of a stack, we can visit the nodes in level-order. Here is the code for level-order traversal: http://ecomputernotes.com
  • 5. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 6. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 7. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 8. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 9. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 10. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 11. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 12. Level-order Traversal void levelorder(TreeNode<int>* treeNode) { Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << &quot; &quot;; if(treeNode->getLeft() != NULL ) q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL ) q.enqueue( treeNode->getRight()); } cout << endl; }  http://ecomputernotes.com
  • 13. Level-order Traversal Queue: 14 Output: 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 14. Level-order Traversal Queue: 4 15 Output: 14 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 15. Level-order Traversal Queue: 15 3 9 Output: 14 4 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 16. Level-order Traversal Queue: 3 9 18 Output: 14 4 15 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 17. Level-order Traversal Queue: 9 18 Output: 14 4 15 3 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 18. Level-order Traversal Queue: 18 7 Output: 14 4 15 3 9 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 19. Level-order Traversal Queue: 7 16 20 Output: 14 4 15 3 9 18 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 20. Level-order Traversal Queue: 16 20 5 Output: 14 4 15 3 9 18 7 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 21. Level-order Traversal Queue: 20 5 17 Output: 14 4 15 3 9 18 7 16 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 22. Level-order Traversal Queue: 5 17 Output: 14 4 15 3 9 18 7 16 20 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 23. Level-order Traversal Queue: 17 Output: 14 4 15 3 9 18 7 16 20 5 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 24. Level-order Traversal Queue: Output: 14 4 15 3 9 18 7 16 20 5 17 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
  • 25. Storing other Type of Data The examples of binary trees so far have been storing integer data in the tree node. This is surely not a requirement. Any type of data can be stored in a tree node. Here, for example, is the C++ code to build a tree with character strings. http://ecomputernotes.com
  • 26. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 27. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 28. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 29. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 30. Binary Search Tree with Strings void wordTree() { TreeNode<char>* root = new TreeNode<char>(); static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; root->setInfo( word[0] ); for(i=1; word[i]; i++ ) insert(root, word[i] ); inorder( root ); cout << endl; }  http://ecomputernotes.com
  • 31. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 32. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 33. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 34. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 35. Binary Search Tree with Strings void insert(TreeNode<char>* root, char* info) { TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }  http://ecomputernotes.com
  • 36. Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); }  http://ecomputernotes.com
  • 37. Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); }  http://ecomputernotes.com
  • 38. Binary Search Tree with Strings if( strcmp(info, p->getInfo()) == 0 ){ cout << &quot;attempt to insert duplicate: &quot; << *info << endl; delete node; } else if( strcmp(info, p->getInfo()) < 0 ) p->setLeft( node ); else p->setRight( node ); }  http://ecomputernotes.com
  • 39. Binary Search Tree with Strings Output: abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket http://ecomputernotes.com
  • 40. Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. http://ecomputernotes.com
  • 41. Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. http://ecomputernotes.com
  • 42. Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. http://ecomputernotes.com
  • 43. Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. Inorder prints the left subtree, then the node finally the right subtree. http://ecomputernotes.com
  • 44. Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. Inorder prints the left subtree, then the node finally the right subtree. Building a BST and doing an inorder traversal leads to a sorting algorithm. http://ecomputernotes.com
  • 45. Binary Search Tree with Strings abandon abash accuse adhere advise babble backup bandit cease chain daily debunk eagle economy fable feeder fetch gain genius jacket Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. This should not come as a surprise if you consider how we built the BST. For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right. Inorder prints the left subtree, then the node finally the right subtree. Building a BST and doing an inorder traversal leads to a sorting algorithm. http://ecomputernotes.com
  • 46. Deleting a node in BST As is common with many data structures, the hardest operation is deletion. Once we have found the node to be deleted, we need to consider several possibilities. If the node is a leaf , it can be deleted immediately. http://ecomputernotes.com
  • 47. Deleting a node in BST If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node and connect to inorder successor. 6 2 4 3 1 8 http://ecomputernotes.com
  • 48. Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8  http://ecomputernotes.com
  • 49. Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8 6 2 3 1 8   http://ecomputernotes.com
  • 50. Deleting a node in BST The complicated case is when the node to be deleted has both left and right subtrees. The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node. http://ecomputernotes.com
  • 51. Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder successor http://ecomputernotes.com
  • 52. Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder successor Inorder successor will be the left-most node in the right subtree of 2. The inorder successor will not have a left child because if it did, that child would be the left-most node. http://ecomputernotes.com
  • 53. Deleting a node in BST Delete(2): copy data from inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
  • 54. Deleting a node in BST Delete(2): remove the inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
  • 55. Deleting a node in BST Delete(2)  6 3 5 4 1 8  6 3 5 3 1 8 4 http://ecomputernotes.com

Editor's Notes

  • #4: End of lecture 14
  • #52: Start lecture 16
  • #56: End of lecture 15